package arth;

import com.sun.xml.internal.ws.util.StringUtils;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

//        String input = "aacabdkacaa";
//        String input = "abacab";
//        String input = "tattarrattat";
        String input = "bb";

        String max = solution.longestPalindrome(input);

        System.out.println(max);
    }


    public String longestPalindrome(String s) {


        char[] chars = s.toCharArray();

       // String maxStr = s.length() > 0 ? s.substring(0,1) : "";

        String maxStr = "";

        for (int i = 0; i < chars.length ; i++) {

            int left = i == 0 ? i : i -1;
            int right = i == chars.length - 1 ?  i : i+1;
            String tmp1 = get(left,i,chars,s);
            String tmp2 = get(left,right,chars,s);

            String tmp = tmp1.length() >= tmp2.length() ? tmp1:tmp2;

            maxStr = maxStr.length() >= tmp.length() ?  maxStr : tmp;
        }


        return maxStr;
    }

    public String get(int left,int right,char[] chars,String s){

        boolean state= false;
        while(chars[left] == chars[right]){
            state = true;
            left--;
            right++;
            if(left < 0 || right > chars.length -1){

                return s.substring(left+1,right);
            }
        }
        if(state) {
            return s.substring(left + 1, right);
        }
        else{
            return s.substring(left, left+1);
        }
    };


}
